package com.victor.zjbj_android.base;

import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;

import com.victor.zjbj_android.utils.retrofit.RetrofitUtils;

import butterknife.ButterKnife;


/**
 * Created by liuwei on 2018/4/19 2017/12/23.
 */

public abstract class BaseActivity extends AppCompatActivity {

    protected RetrofitUtils mRetrofit;

    protected abstract int getResourcesId();

    protected abstract void init(Bundle savedInstanceState);


    //dialog,临时使用
    private ProgressDialog loadDialog;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getResourcesId());
        setStatusBarFullTransparent();
        setFitSystemWindow(true);
        mRetrofit = RetrofitUtils.getInstance(getApplicationContext()).createBaseApi();
        ButterKnife.bind(this);
        init(savedInstanceState);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        dismissDialog();
        loadDialog = null;
    }

    public void showLoadDialog(String message, boolean cancelable) {
        if (loadDialog == null) {
            loadDialog = ProgressDialog.show(this, "", message);
            loadDialog.setCancelable(cancelable);
        } else if (!loadDialog.isShowing()) {
            loadDialog.show();
            loadDialog.setCancelable(cancelable);
        }
    }

    public ProgressDialog showReturnLoadDialog(String message) {
        if (loadDialog == null) {
            loadDialog = ProgressDialog.show(this, "", message);
        } else if (!loadDialog.isShowing()) {
            loadDialog.show();
        }
        return loadDialog;
    }

    public void showLoadDialog() {
        showLoadDialog("loding...", true);
    }

    public void showCancleLoadDialog() {
        showLoadDialog("loding...", true);
    }

    public void showCancleLoadDialog(String message) {
        showLoadDialog(message, true);
    }

    public void dismissDialog() {
        if (loadDialog != null && loadDialog.isShowing()) {
            loadDialog.dismiss();
        }
    }

    /**
     * 强制隐藏键盘
     */
    public void forceHideIM() {
        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (inputMethodManager == null) {
            return;
        }
        View decorView = getWindow().getDecorView();
        inputMethodManager.hideSoftInputFromWindow(decorView.getWindowToken(), 0);
    }

    /**
     * 全透状态栏
     */
    protected void setStatusBarFullTransparent() {
        if (Build.VERSION.SDK_INT >= 21) {//21表示5.0
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        } else if (Build.VERSION.SDK_INT >= 19) {//19表示4.4
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //虚拟键盘也透明
            //getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
    }

    /**
     * 如果需要内容紧贴着StatusBar
     * 应该在对应的xml布局文件中，设置根布局fitsSystemWindows=true。
     */
    private View contentViewGroup;

    protected void setFitSystemWindow(boolean fitSystemWindow) {
        if (contentViewGroup == null) {
            contentViewGroup = ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0);
        }
        contentViewGroup.setFitsSystemWindows(fitSystemWindow);
    }


}
